#!/usr/bin/python2

"""
Copyright (c) 2006, David Forsythe
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the ob-2-fb nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

import sys
import os
import shutil
import errno

class themeConvert:
	def __init__(self):
		self.convert = {}
		self.convert[0] = ['fluxbox', 'openbox']
		self.convert[1] = ['.imageColor', '.picColor']
		self.convert[2] = ['menu.frame','menu.items']
		self.convert[3] = ['menu.hilite','menu.selected']
		self.convert[4] = ['borderColor','border.color']
		self.convert[5] = ['imageColor','image.color']
		self.convert[6] = ['textColor','text.color']
		self.convert[7] = ['interlaceColor','interlace.color']
		self.convert[8] = ['window.focus.font','window.active.label.text.font']
		self.convert[9] = ['window.unfocus.font','window.inactive.label.text.font']
		self.convert[10] = ['window.justify','window.label.justify']
		self.convert[11] = ['menu.frame.disableColor','menu.disabled.textColor']
		self.convert[12] = ['window.label.focus.font','window.active.label.text.font']
		self.convert[13] = ['window.label.unfocus.font','window.inactive.label.text.font']
		self.convert[14] = ['window.label.justify','window.label.text.justify']
		self.convert[15] = ['menu.title.font','menu.title.text.font']
		self.convert[16] = ['menu.title.justify','menu.title.text.justify']
		self.convert[17] = ['menuOverlap','menu.overlap']
		self.convert[18] = ['handleWidth','window.handle.width']
		self.convert[19] = ['borderWidth','border.width']
		self.convert[20] = ['bevelWidth','padding.width']
		self.convert[21] = ['frameWidth','window.client.padding.width']
		self.convert[22] = ['window.frame.focusColor','window.active.client.color']
		self.convert[23] = ['window.frame.unfocusColor','window.inactive.client.color']
		self.convert[24] = ['window.title.focus','window.active.title.bg']
		self.convert[25] = ['window.title.unfocus','window.inactive.title.bg']
		self.convert[26] = ['window.label.focus','window.active.label.bg']
		self.convert[27] = ['window.label.unfocus','window.inactive.label.bg']
		self.convert[28] = ['window.handle.focus','window.active.handle.bg']
		self.convert[29] = ['window.handle.unfocus','window.inactive.handle.bg']
		self.convert[30] = ['window.grip.focus','window.active.grip.bg']
		self.convert[31] = ['window.grip.unfocus','window.inactive.grip.bg']
		self.convert[32] = ['menu.items','menu.items.bg']
		self.convert[33] = ['menu.title','menu.title.bg']
		self.convert[34] = ['menu.selected','menu.items.active.bg']
		self.convert[35] = ['window.title.focus','window.active.title.bg']
		self.convert[36] = ['window.label.focus','window.active.label.bg']
		self.convert[37] = ['window.title.unfocus','window.inactive.title.bg']
		self.convert[38] = ['window.label.unfocus','window.inactive.label.bg']
		self.convert[39] = ['window.button.disabled.focus','window.active.button.disabled.bg']
		self.convert[40] = ['window.button.disabled.unfocus','window.inactive.button.disabled.bg']
		self.convert[41] = ['window.button.pressed.focus','window.active.button.pressed.bg']
		self.convert[41] = ['window.button.pressed.unfocus','window.inactive.button.pressed.bg']
		self.convert[42] = ['window.button.toggled.focus','window.active.button.toggled.bg']
		self.convert[43] = ['window.button.toggled.unfocus','window.inactive.button.toggled.bg']
		self.convert[44] = ['window.button.focus','window.active.button.unpressed.bg']
		self.convert[45] = ['window.button.unfocus','window.inactive.button.unpressed.bg']
		self.convert[46] = ['window.button.hover.focus','window.active.button.hover.bg']
		self.convert[47] = ['window.button.hover.unfocus','window.inactive.button.hover.bg']
		
		self.ob2fbClaim = '!! This theme was created by converting an OpenBox3 theme using ob2fb.  Visit **site** for more information\n'
		self.iconStuff = 'window.close.pixmap: close.xpm\nwindow.close.pressed.pixmap: close_pressed.xpm\nwindow.close.unfocus.pixmap: close_disabled.xpm\nwindow.iconify.pixmap:iconify.xpm\nwindow.iconify.pressed.pixmap: icon_pressed.xpm\nwindow.iconify.unfocus.pixmap: iconify_disabledk.xpm\nwindow.maximize.pixmap: max.xpm\nwindow.maximize.pressed.pixmap: max_pressed.xpm\nwindow.maximize.unfocus.pixmap: max_disabled.xpm\n'

	def openFile(self, filename):
		self.themerc = open(filename, 'r')
		self.convertNote = 'Theme converted from ' + filename + '\n'
		self.outList = [self.ob2fbClaim, self.convertNote]

	def translate(self, line):
		self.outLine = str(line)
		for count in range(len(self.convert)):
			lineStr = str(line)
			obStr = str(self.convert[count][1])
			fbStr = str(self.convert[count][0])
			if lineStr.find(obStr) != -1:
				self.outLine = lineStr.replace(obStr, fbStr)
				if self.verbose == 1:
					line = line.replace('\n', ' ')
					print line + '-> ' + self.outLine					
			
	def scrollFile(self):
		for line in self.themerc:
			self.translate(line)
			self.outList.append(self.outLine)
			
	def createTheme(self):
		themeDir = 'new.fluxbox.theme/'
		xbmNames = ['max.xpm', 'max_toggled.xpm', 'max_disabled.xpm', 'max_hover.xpm', 'max_pressed.xpm', 'iconify.xpm', 'iconify_disabled.xpm', 'iconify_hover.xpm', 'iconify_pressed.xpm', 'desk.xpm', 'desk_toggled.xpm', 'desk_disabled.xpm', 'desk_hovered.xpm', 'desk_pressed.xpm', 'shade.xpm', 'shade_toggled.xpm', 'shade_disabled.xpm', 'shade_hover.xpm', 'shade_pressed.xpm', 'close/xbm', 'close_disabled.xpm', 'close_hover.xpm', 'close_pressed.xpm', 'bullet.xpm']
		try:
			os.makedirs(themeDir+'pixmaps/')
		except OSError, err:
			if err.errno == errno.EEXIST:
				if os.path.isdir(themeDir+'pixmaps/'):
					print "...directory already exists.  Move your old themes!"
				else:
					print "...file already exists, but not a directory."
					raise
			else:
				raise
		self.themecfg = open(themeDir+'theme.cfg', 'w')
		self.outList.append(self.iconStuff)
		for line in self.outList:
			out = str(line)
			self.themecfg.write(out)
		for bitmap in xbmNames:
			if os.path.exists(bitmap) == 1:
				shutil.copy(bitmap, themeDir+'pixmaps/'+bitmap)		
	
	def setopts(self, verbopt, logopt):
		self.verbose = verbopt
		
def showhelp():	
	print "ob2fb version 0.0.1"
	print ""
	print "ob2fb -[v|h] <themerc>"
	print ""
	print "-v | --verbose: verbose execution"
	print "-h | --help: show help"
			
def main():
	verbose = 0
	logging = 0
	help = 0
	for arg in sys.argv:
		if arg == '-v':
			verbose = 1
		elif arg == '--verbose':
			verbose = 1
		elif arg == '-h':
			help = 1
		elif arg == '--help':
			help = 1
		else:
			themerc = arg
	if help == 1:
		showhelp()
		sys.exit()
	theme = themeConvert()
	theme.setopts(verbose, logging)
	theme.openFile(themerc)
	theme.scrollFile()
	theme.createTheme()
	print 'done!'
	
if __name__ == '__main__': main()